package com.fangwaii.core.session.defaults;


import com.fangwaii.core.datasource.Connection;
import com.fangwaii.core.datasource.DataSource;
import com.fangwaii.core.datasource.DataSourceFactory;
import com.fangwaii.core.datasource.unpooled.UnpooledDataSourceFactory;
import com.fangwaii.core.executor.Executor;
import com.fangwaii.core.session.Configuration;
import com.fangwaii.core.session.GatewaySession;
import com.fangwaii.core.session.GatewaySessionFactory;

/**
 * @author 小傅哥，微信：fustack
 * @description 默认网关会话工厂
 * @github https://github.com/fuzhengwei
 * @Copyright 公众号：bugstack虫洞栈 | 博客：https://bugstack.cn - 沉淀、分享、成长，让自己和他人都能有所收获！
 */
public class DefaultGatewaySessionFactory implements GatewaySessionFactory {

    private final Configuration configuration;

    public DefaultGatewaySessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public GatewaySession openSession(String uri) {
        // 获取数据源连接信息：这里把 Dubbo、HTTP 抽象为一种连接资源
        DataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
        dataSourceFactory.setProperties(configuration, uri);
        DataSource dataSource = dataSourceFactory.getDataSource();
        Connection connection = dataSource.getConnection();
        Executor executor = configuration.newExecutor(connection);

        return new DefaultGatewaySession(configuration, uri, executor);
    }

}
